//
//  UIHelper.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/11/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "UIHelper.h"
#import "PMGlobal.h"
static UIHelper *_gInstance = nil;

@interface UIHelper(){
}

@end

@implementation UIHelper

+ (UIHelper *)getInstance {
    @synchronized( [UIHelper class] ){
        if(!_gInstance)
            _gInstance = [[self alloc] init];
        return _gInstance;
    }
    return nil;
}

+ (id)alloc {
    @synchronized ([UIHelper class]){
        NSAssert(_gInstance == nil,
                 @"Attempted to allocated a second instance");
        _gInstance = [super alloc];
        return _gInstance;
    }
    return nil;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}


//------------------------------------------------------------------------------------------------------
- (UIImage *)getBundleImage:(NSString *)fileName{
    
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *customerPath = [bundlePath stringByAppendingPathComponent:CUSTOMER_PATH];
    
    NSString *filePath = [customerPath stringByAppendingPathComponent:fileName];
    if (![[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        filePath = [bundlePath stringByAppendingPathComponent:fileName];
    }
    
    return [UIImage imageWithContentsOfFile:filePath];
}


- (UIColor *)getRandomColor{
    return [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
}

- (UIColor *)getColorbyRed:(int)red Green:(int)green Blue:(int)blue Alpha:(int)alpha{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0];
}

#pragma mark UIButton
- (UIButton *)createButtonByImage:(UIImage *)image{
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    b.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    [b setImage:image forState:UIControlStateNormal];
    return b;
}
- (UIButton *)createButtonByImageName:(NSString *)fileName{
    UIImage *img = [self getBundleImage:fileName];
    UIButton *b = [self createButtonByImage:img];    
    return b;
}
- (UIButton *)createButtonByImagePath:(NSString *)filePath{
    UIImage *img = [UIImage imageWithContentsOfFile:filePath];
    UIButton *b = [self createButtonByImage:img];
    return b;
}

- (UIButton *)createButtonByFrame:(CGRect)frame{
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    b.frame = frame;
    return b;
}

- (void)setButtonVerticalArrange:(UIButton *)button{
    UIImage *img = [button imageForState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, button.height-img.size.height, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(img.size.height, -img.size.width, 0, 0);
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
}

#pragma mark UIImageView
- (UIImageView *)createViewByImage:(UIImage *)image{
    UIImageView *v = [[UIImageView alloc] initWithImage:image];
    return v;
}
- (UIImageView *)createViewByImageName:(NSString *)fileName{
    UIImage *img = [self getBundleImage:fileName];
    UIImageView *v = [self createViewByImage:img];
    return v;
}
- (UIImageView *)createViewByImagePath:(NSString *)filePath{
    UIImage *img = [UIImage imageWithContentsOfFile:filePath];
    UIImageView *v = [self createViewByImage:img];    
    return v;
}

#pragma mark UILabel
- (UILabel *)createLabel:(NSString *)content withWidth:(float)width withFont:(UIFont *)font{
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT)];
    UILabel *l = [self createLabel:CGRectMake(0, 0, width, size.height)];
    l.font = font;
    l.text = content;
    return l;
}

- (UILabel *)createLabel:(CGRect)frame{
    UILabel *l = [[UILabel alloc] initWithFrame:frame];
    l.backgroundColor = [UIColor clearColor];
    return l;
}

@end
